package com.chenfan.mcn.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.chenfan.mcn.clients.baseinfo.BaseInfoClient;
import com.chenfan.mcn.dao.ContractExcutionOrderMapper;
import com.chenfan.mcn.dao.StarBillingHeaderMapper;
import com.chenfan.mcn.enums.ApprovalStatusEnum;
import com.chenfan.mcn.enums.LogBusinessTypeEnum;
import com.chenfan.mcn.event.EventPublisher;
import com.chenfan.mcn.event.OperateLogEvent;
import com.chenfan.mcn.model.ContractExcutionOrder;
import com.chenfan.mcn.model.StarBillingHeader;
import com.chenfan.mcn.service.StarBillingHeaderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author liuyang
 * @Date 2021/12/9
 * @Time 17:31
 * @Version V1.0
 **/
@Service
public class StarBillingHeaderServiceImpl implements StarBillingHeaderService {

    @Autowired
    private StarBillingHeaderMapper starBillingHeaderMapper;

    @Autowired
    private BaseInfoClient baseInfoClient;

    @Autowired
    private ContractExcutionOrderMapper contractExcutionOrderMapper;

    @Override
    public Boolean modifyEnableStatus(Long id, Integer status) {
        StarBillingHeader starBillingHeader = starBillingHeaderMapper.selectById(id);
        StarBillingHeader billingHeader = new StarBillingHeader();
        BeanUtils.copyProperties(starBillingHeader, billingHeader);
        starBillingHeader.setIsEnable(status);
        boolean result = this.updateById(starBillingHeader);
        if(result) {
            //操作日志
            EventPublisher.post(OperateLogEvent.builder().businessId(starBillingHeader.getStarId())
                    .businessType(LogBusinessTypeEnum.STAR_BILLING_HEADER)
                    .compare(starBillingHeader, billingHeader)
                    .build());
        }
        return result;
    }

    @Override
    public StarBillingHeader getBillingHeader(Long starId, String headerName) {
        return starBillingHeaderMapper.selectOne(Wrappers.lambdaQuery(StarBillingHeader.class)
                .eq(StarBillingHeader::getStarId, starId)
                .eq(StarBillingHeader::getHeaderName, headerName)
                .eq(StarBillingHeader::getIsDelete, Boolean.FALSE)
                .eq(StarBillingHeader::getIsEnable, Boolean.FALSE));
    }

    @Override
    public Boolean delete(Long id) {
        List<ContractExcutionOrder> executionOrderList = contractExcutionOrderMapper.selectList(Wrappers.lambdaQuery(ContractExcutionOrder.class)
                .eq(ContractExcutionOrder::getStarBillingHeaderId, id)
                .ne(ContractExcutionOrder::getApprovalStatus, ApprovalStatusEnum.INVALID.code())
                .eq(ContractExcutionOrder::getIsDelete, Boolean.FALSE));
        Assert.isTrue(CollectionUtils.isEmpty(executionOrderList), "该开票已经关联执行单" + executionOrderList.stream().map(ContractExcutionOrder::getExcuteCode).collect(Collectors.toList()) + "不允许删除。");
        StarBillingHeader billingHeader = starBillingHeaderMapper.selectById(id);
        billingHeader.setIsDelete(1);
        return this.updateById(billingHeader);
    }
}
